import numpy
import bisect

'''
import sys
import os

# 添加路径到 sys.path
module_path = os.path.abspath(r'D:/git/gphelper')
if module_path not in sys.path:
    sys.path.append(module_path)

# 现在可以导入该路径下的模块
import gphelper
'''
def sample_subarray(arr, start_percent, end_percent):
    # 计算数组长度的3%和5%
    length = len(arr)
    min_length = int(start_percent * length)  # 3%的长度
    max_length = int(end_percent * length)  # 5%的长度
    
    # 确保最小长度不大于最大长度，且至少为1
    if min_length == 0:
        min_length = 1
    if max_length < min_length:
        max_length = min_length
    
    start_index = min_length
    subarray_length = max_length - start_index
    # 抽取子数组
    subarray = arr[start_index:start_index + subarray_length]
    #print(subarray[0])
    return subarray, start_index, subarray_length

def sample_subarray_by_idx(arr, start_index, subarray_length):
    # 抽取子数组
    subarray = arr[start_index:start_index + subarray_length]
    return subarray, start_index, subarray_length


def find_indices_in_range(arr, min_val, max_val):
    left = find_start_index(arr, min_val)
    right = find_end_index(arr, max_val)
    return list(range(left, right + 1))

def find_start_index(arr, min_val):
    left, right = 0, len(arr) - 1
    while left < right:
        mid = (left + right) // 2
        if arr[mid] < min_val:
            left = mid + 1
        else:
            right = mid
    return left if arr[left] >= min_val else len(arr)

def find_end_index(arr, max_val):
    left, right = 0, len(arr) - 1
    while left < right:
        mid = (left + right + 1) // 2  # Bias to the right
        if arr[mid] > max_val:
            right = mid - 1
        else:
            left = mid
    return left if arr[left] <= max_val else -1

def sample_subarray_between(arr, value_min, value_max):
    idxOfMin = find_start_index(arr, value_min)
    idxOfMax = find_end_index(arr, value_max)
    
    return arr[idxOfMin:idxOfMax], idxOfMin, idxOfMax-idxOfMin

def fixed_step_sampling(array, step):
    """
    从数组中按照固定步长进行抽样。
    :param array: 原始数组
    :param step: 步长
    :return: 新数组
    """
    return array[::step]